package ch07;

import java.util.HashSet;
import java.util.Random;

public class HashSetMain2 {
    private static void test1(){
        // 去重
        String[] names = {"曹操","刘备","关羽","张飞","赵云","孙权","诸葛亮"};
        Random random = new Random();
//        for (int i=0;i<5;i++){
//            System.out.println(names[random.nextInt(names.length)]);
//        }
        HashSet<String> hashSet = new HashSet<>();
        while(hashSet.size()<5){
            hashSet.add(names[random.nextInt(names.length)]);//随机+去重
        }
        for (String name:hashSet) {
            System.out.println(name);
        }
    }
    private static void test2(){
        // 去重
        Student[] students = new Student[]{
                new Student(1,"刘备",28),
                new Student(2,"关羽",27),
                new Student(3,"张飞",25),
                new Student(4,"赵云",24),
                new Student(5,"曹操",38),
                new Student(6,"孙权",21),
                new Student(7,"诸葛亮",24),
                new Student(8,"诸葛亮",24)
        };
        Random random = new Random();

        HashSet<Student> hashSet = new HashSet<>();
        while(hashSet.size()<5){
            hashSet.add(students[random.nextInt(students.length)]);//随机+去重
        }
        for (Student student:hashSet) {
            System.out.println(student);
        }
    }
    public static void main(String[] args) {
//        test1();
        test2();
    }
}
